<%@page import="com.jfinal.kit.PropKit" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" import="cn.com.dashihui.web.common.OrderCode" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <title>订单管理</title>
    <jsp:include page="../../include/header.jsp"></jsp:include>
    <style type="text/css">
			table th{
						text-align: center;
			            white-space: nowrap;
			        }
			table td{
						text-align: center;
			            white-space: nowrap;
			        }
			  table{
			         empty-cells:show; 
			         border-collapse: collapse;
			         margin:0 auto;
			        }
					 	  /*排序 */
			.querySort{
			   position:relative;
			}
			.down{
			   position: absolute;
			   top:12px;
			   display:inline-block;
			   width:10px;
			   height:12px;
			   margin-left: 8px;
			   background: url("${BASE_PATH}/static/images/sprite-arrow.png") 0 61px;
			}
		  .sort .querySort:NTH-OF-TYPE(2) .down{
			    top:-3px;
			    transform: rotateX(180deg);
			}
			.querySort:hover .down{
			   background: url("${BASE_PATH}/static/images/sprite-arrow.png") 0 21px;
			}
			
		  .downSort{
			    position: absolute;
			    top: 3px;
			    left: 3px;
			    width: 18px;
			    height: 24px;
			    opacity: 0;
			    cursor:pointer;
			}
		    .upSort {
			    position: absolute;
			    top: -15px;
			    left: 3px;
			    width: 18px;
			    height: 24px;
			    opacity: 0; 
			    cursor:pointer;
			}
            
		</style>
</head>
<body>
<div id="wrapper"  class="row">
	
			<div class="col-lg-12">
				<h1 class="page-header">订单统计</h1>
			</div>
		<!-- 搜索框 -->
			<div class="title-top col-lg-12">
				<label class="search-label">订单编号：</label><input type="text" id="sOrderNum" value="" class="form-control search-input width200"  maxlength="21">
				<label class="search-label">买家电话：</label><input type="text" id="sTel" value="" class="form-control search-input width200" maxlength="13">
				<label class="search-label">下单时间：</label><input type="text" id="sBeginDate" autocomplete="off"  value="" class="form-control search-input width120 datetimepicker" data-format="yyyy-mm-dd" >
				<label class="search-label">至</label><input type="text" id="sEndDate" autocomplete="off" value="" class="form-control search-input width120 datetimepicker" data-format="yyyy-mm-dd" >
				<div class="clearfix"></div>
				<label class="search-label">订单状态：</label>
				<div class="search-input">
					<select id="sState" class="selectpicker" data-width="180">
						<option value="0">全部</option>
						<option value="1">待付款</option>
						<option value="2">待出库</option>
						<option value="3">待评价</option>
						<option value="4">已完成</option>
						<option value="5">已删除</option>
						<option value="6">待收货</option>
						<option value="9">已过期</option>
					</select>
				</div>
				<label class="search-label">免单状态：</label>
				<div class="search-input">
					<select id="sIsFree" class="selectpicker" data-width="180">
						<option value="9">全部</option>
						<option value="0">无需免单</option>
						<option value="1">待免单</option>
						<option value="2">免单中</option>
						<option value="3">已免单</option>
					</select>
				</div>
				<label class="search-label">选择商品：</label>
				<div class="search-input">
					<select id="sSelectGoods" class="selectpicker pull-left p-l-5" data-width="180" data-width="14%">
							<option value="">全部</option>
						<c:forEach items="${listGoods }" var="goods">
							<option value="${goods.id }">${goods.id}-${goods.name}</option>
						</c:forEach>
					</select>
				</div>
				<label class="search-label">选择公司：</label>
				<select id="sSellerId" class="selectpicker pull-left p-l-5" multiple="multiple" data-width="14%">
						<c:forEach items="${list }" var="seller">
							<option value="${seller.sellerId }">${seller.sellerName }</option>
						</c:forEach>
					</select>
				<label class="search-label">选择分公司：</label>
				<select id="sStoreId" class="selectpicker pull-left p-l-5" multiple="multiple" data-width="14%" >
					<c:forEach items="${listStore }" var="store">
						<option value="${store.storeid }">${store.title }</option>
					</c:forEach>
				</select>
				<button class="btn btn-success search-btn" onclick="query();">确定</button>
				<button class="btn btn-success search-btn" onclick="reset();">重置</button>
				<button class="btn btn-default search-btn" title="刷新" onclick="window.location.reload();"> 刷新</button>
				
				<div class="clearfix"></div>
				<shiro:hasPermission name="bus:order:doExcel">
						<button onclick="doExcelgb();"  class="btn btn-success pull-right">导出</button>
				</shiro:hasPermission>
			</div>
			<div class="col-lg-12">
				<div class="row">
					<div class="text-muted single-line-text pull-left">共 <font color="#428bca" id="dataCount">0</font> 条记录</div>
					<div class="col-lg-6">
						<div class="text-muted single-line-text pull-left">
							<span class="m-l-5"> 
								总累计现金<font color="#428bca" id="amount">0.00</font>元
							</span>
							<span class="m-l-5"> 
								总实际现金收入<font color="#428bca" id="shiji">0.00</font>元
							</span>
						</div>
					</div>
				</div>
				<div class="table-responsive panel panel-default" >
					<table id="dataTable" class="table table-hover table-bordered" style="overflow:scroll;">
						<thead>
							<tr>
								<th width="100px;">状态</th>
								<th width="100px;">免单</th>
								<th width="200px;">订单号</th>
								<th width="80px;">会员姓名</th>
								<th width="150px;">会员卡号</th>
								<th width="120px;">联系方式</th>
								<th width="120px;">下单公司</th>
								<th width="100px;">下单分公司</th>
								<th width="200px;">下单时间</th>
								<th width="80px;">订单总价值</th>
								<th width="200px;">支付时间</th>
							</tr>
			            </thead>
			            <tbody id="dataList"></tbody>
			        </table>
				</div>
		        <div class="col-lg-12">
        			<div class = "pull-right">
	        			<div class="text-muted  pull-left" style="margin-top:20px; margin-right:18px;">
							<select id="pageSize" name="pageSize" class="selectpicker" data-width="100" onchange="query()" >
		    					<option value="10" selected = "selected">10条/页</option>
								<option value="15">15条/页</option>
								<option value="20">20条/页</option>
								<option value="50">50条/页</option>
								<option value="100">100条/页</option>
		    				</select>
						</div>
		        		<ul id="dataPagination" class="pagination-sm pull-right"></ul>
		        	</div>
	        	</div>
			</div>
	
</div>
<jsp:include page="../../include/javascripts.jsp"></jsp:include>
<!-- 异步加载下一页数据后，用模板渲染 -->
<script type="text/html" id="dataTpl">
		{{each list as item}}
				 <tr id="item{{item.orderNum}}" data-id="{{item.orderNum}}">
					{{if item.orderState == 1}}
						<td>待付款</td>
					{{else if item.orderState == 2}}
						<td>
					<button type="button" class="btn " onclick="toAddWaybill('{{item.orderNum}}')" title="待出库"><font color="#EE2C2C" ="dataCount">待出库</font></button>
						</td>
					{{else if item.orderState == 3}}
						<td>待评价</td>
					{{else if item.orderState == 4}}
						<td>已完成</td>
					{{else if item.orderState == 5}}
						<td>删除</td>	
					{{else if item.orderState == 6}}
						<td>	
						<button type="button" class="btn " onclick="WBDetail('{{item.orderNum}}')" title="待收货"><font color="#40a9ff" ="dataCount">待收货</font></button>
						</td>	
					{{/if}}
					{{if item.isFree == 0}}
						<td>无需免单</td>
					{{else if item.isFree == 1}}
						<td>待免单</td>
					{{else if item.isFree == 2}}
						<td>免单中</td>
					{{else if item.isFree == 3}}
						<td>已免单</td>
					{{/if}}
					<td><a href="javascript:showOrder('{{item.orderNum}}');"><font color="#40a9ff" ="dataCount">{{item.orderNum}}</font></a></td>
					<td><a href="javascript:showUser('{{item.uid}}');"><font color="#40a9ff" id="dataCount">{{item.name}}</font></a></td>
					<td>{{item.card}}</td>
					<td>{{item.tel}}</td>
					<td>{{item.sellerName}}</td>
					<td>{{item.xdstore}}</td>
					<td>{{item.startDate}}</td>
					<td>{{item.amount}}</td>
					<td>{{item.payDate}}
						{{if !item.payDate}}此订单尚未支付或已删除{{/if}}
					</td>
				</tr>
		{{/each}}
</script>
<script type="text/javascript">
var dataPaginator;
var userId="${userId}";
//物流信息
var toAddWaybillDialog;
function toAddWaybill(orderNum){
	toAddWaybillDialog = Kit.dialog("添加订单物流信息","${BASE_PATH}/bus/order/toAddWaybillgb?orderNum="+orderNum,{size:'size-wide',closable:true}).open();
}
//物流详情
var WBDetailDialog;
function WBDetail(orderNum){
	WBDetailDialog = Kit.dialog("添加订单物流信息","${BASE_PATH}/bus/order/WBDetail?orderNum="+orderNum,{size:'size-wide',closable:true}).open();
}
$(query);
function query(){
	var pageSize=$("#pageSize").val();
	var sSellerId = $("#sSellerId").val(),sStoreId = $("#sStoreId").val();
	var sell,store;
	if(sSellerId !=null && sSellerId !="" && sSellerId !=undefined){
		sell=sSellerId.join("','");
	}
	if(sStoreId !=null && sStoreId !="" && sStoreId !=undefined){
		store=sStoreId.join("','");
	}
	var params = {
		pageSize:pageSize,
		orderNum:$("#sOrderNum").val(),
		beginDate:$("#sBeginDate").val(),
		endDate:$("#sEndDate").val(),
		tel:$("#sTel").val(),
		selectGoods:$("#sSelectGoods").val(),
		isFree:$("#sIsFree").val(),
		state:$("#sState").val(),
		sType:$("#sType").val(),
		sellerId:sell,
		storeId:store,
	};
	if(dataPaginator){
		dataPaginator.destroy();
	}
	if(!userId){
		dataPaginator = Kit.pagination("#dataPagination","${BASE_PATH}/bus/order/pagegb",params,function(result){
			//设置显示最新的数据数量
			$("#dataCount").html(result.object.totalRow);
			//根据模板渲染数据并填充
			$("#dataList").empty().append(template("dataTpl",result.object));
			findTotalgb();
		});
		
	}else{
		//根据用户Id查询此用户所有订单
		dataPaginator = Kit.pagination("#dataPagination","${BASE_PATH}/bus/order/doConvert",{userId:userId},function(result){
			//设置显示最新的数据数量
			$("#dataCount").html(result.object.totalRow);
			//根据模板渲染数据并填充
			$("#dataList").empty().append(template("dataTpl",result.object));
		});
	}
	
}
//显示会员详细信息
function showUser(id){
	if(id){
		showDialog = Kit.dialog("会员详情信息","${BASE_PATH}/bus/order/userdetail/"+id,{size:'size-wide',closable:true}).open();
	}else{
		//根据用户Id查询此用户所有订单
		showDialog = Kit.dialog("会员详情信息","${BASE_PATH}/bus/order/userdetail/"+userId,{size:'size-wide',closable:true}).open();
	}
}
//显示订单详细信息
var showOrderDialog;
function showOrder(orderNum){
	showOrderDialog = Kit.dialog("查看订单","${BASE_PATH}/bus/order/detail?orderNum="+orderNum,{size:'size-wide',closable:true}).open();
}
/**
 * 此排序与默认排序冲突故分成二方法
 */
 function query1(index){
	
	   findTotalgb();
	   var pageSize=$("#pageSize").val();
		var  sSellerId = $("#sSellerId").val(),sStoreId = $("#sStoreId").val();
		var sell,store;
		if(sSellerId !=null && sSellerId !="" && sSellerId !=undefined){
			sell=sSellerId.join("','");
		}
		if(sStoreId !=null && sStoreId !="" && sStoreId !=undefined){
			store=sStoreId.join("','");
		}
		var params = {
			pageSize:pageSize,
			orderNum:$("#sOrderNum").val(),
			beginDate:$("#sBeginDate").val(),
			endDate:$("#sEndDate").val(),
			tel:$("#sTel").val(),
			selectGoods:$("#sSelectGoods").val(),
			isFree:$("#sIsFree").val(),
			state:$("#sState").val(),
			sType:$("#sType").val(),
			sellerId:sell,
			storeId:store,
			index:index
		};
		if(dataPaginator){
			dataPaginator.destroy();
		}
		dataPaginator = Kit.pagination("#dataPagination","${BASE_PATH}/bus/order/page1",params,function(result){
			//设置显示最新的数据数量
			$("#dataCount").html(result.object.totalRow);
			//根据模板渲染数据并填充
			$("#dataList").empty().append(template("dataTpl",result.object));
			
		});
	}
function reset(){
	$("#sOrderNum").val("");$("#sBeginDate").val("");$("#sEndDate").val("");$("#sTel").val("");
	$("#sSellerId").val("");$("#sSellerId").trigger("change");  $("#sStoreId").val("");$("#sStoreId").trigger("change");  $("#sState").val("0");$("#sState").trigger("change");
	$("#sStoreId").selectpicker("refresh"); $("#sSellerId").selectpicker("refresh");
	$("#sType").val("0");$("#sType").selectpicker("refresh");
}
var showOrderDialog;
function showOrder(orderNum){
	showOrderDialog = Kit.dialog("查看订单","${BASE_PATH}/bus/order/detailgb?orderNum="+orderNum,{size:'size-wide',closable:true}).open();
}
function doExcelgb(){
	var orderNum=$("#sOrderNum").val(),beginDate=$("#sBeginDate").val(), endDate=$("#sEndDate").val(),tel=$("#sTel").val(),state=$("#sState").val(),sSellerId=$("#sSellerId").val(),sStoreId=$("#sStoreId").val();
	var sell="",store="";
	if(sSellerId !=null && sSellerId !="" && sSellerId !=undefined){
		sell=sSellerId.join("','");
	}
	if(sStoreId !=null && sStoreId !="" && sStoreId !=undefined){
		store=sStoreId.join("','");
	}
	if(!userId){
		window.location.href="${BASE_PATH}/bus/order/doExcelgb?orderNum="+orderNum+"&beginDate="+beginDate+"&endDate="+endDate+"&tel="+tel+"&state="+state+"&sellerId="+sell+"&storeId="+store;
	}else{
		//根据用户ID导出
		window.location.href="${BASE_PATH}/bus/order/doConvertExcel?userId="+userId;

	}
}
$("#sSellerId").change(function(){
	 var sSellerId=[];
	  sSellerId=$("#sSellerId").val();
	  $.ajax({
               type: "POST",
               url: "${BASE_PATH}/bus/goods/base/chooseStore",
               data: {"sSellerId" : sSellerId},
               dataType: "json",
               traditional: true,
               success: function(data){
              	 $("#sStoreId").empty();
                         for(var i=0; i<data.object.length;i++){
                        	 $("#sStoreId").append("<option value='"+data.object[i].storeid+"'>"+data.object[i].title+"</option>");
                         }
                         $("#sStoreId").selectpicker("refresh");
                     }
           });
      }); 
$("#sBeginDate").datetimepicker({
    format: 'yyyy-mm-dd hh:ii:ss',
    minuteStep:1,
    minView:'hour',
    language: 'zh-CN',
    pickerPosition:'bottom-right',
    autoclose:true,
}).on("click",function(){
	var d2=new Date();
	var d=$("#sEndDate").val();
	if(d==""){
		 $("#sBeginDate").datetimepicker("setEndDate",d2);
	}else{
		var d1=new Date(d); 
		d1.setDate(d1.getDate()-1);
		if(d1>d2){
	  	 	 $("#sBeginDate").datetimepicker("setEndDate",d2);
		}else{
			 $("#sBeginDate").datetimepicker("setEndDate",d1);
		}
	}
});

$("#sEndDate").datetimepicker({
    format: 'yyyy-mm-dd hh:ii:ss',
    minuteStep:1,
    minView:'hour',
    language: 'zh-CN',
    autoclose:true,
	}).on("click",function(){
		var d=$("#sBeginDate").val();
		var d1=new Date(d); 
		d1.setDate(d1.getDate()+1); 
		$("#sEndDate").datetimepicker("setStartDate",d1);
	});
	
 $(document).on("click","#sEndDate",function(){
    	$('#sEndDate').datetimepicker('show');
    });

$(document).on("click","#sBeginDate",function(){
	$('#sBeginDate').datetimepicker('show');
});

/**
 * 统计总所需积分和总现金价格
 */
function findTotalgb(){
	var sSellerId = $("#sSellerId").val(),sStoreId = $("#sStoreId").val();
	var sell,store;
	if(sSellerId !=null && sSellerId !="" && sSellerId !=undefined){
		sell=sSellerId.join("','");
	}
	if(sStoreId !=null && sStoreId !="" && sStoreId !=undefined){
		store=sStoreId.join("','");
	}
	var params = {
		pageSize:10,
		beginDate:$("#sBeginDate").val(),
		endDate:$("#sEndDate").val(),
		orderNum:$("#sOrderNum").val(),
		isFree:$("#sIsFree").val(),
		tel:$("#sTel").val(),
		state:$("#sState").val(),
		sType:$("#sType").val(),
		sellerId:sell,
		storeId:store,
	};
	$.post("${BASE_PATH}/bus/order/findTotalgb",params,function(result){
		if(result.flag==0){
/* 			$("#payPoint").text(result.object.payPoint);//总所需积分
 */			$("#amount").text(result.object.amount.toFixed(2));//总累计现金
/* 			$("#tuikuan").text(result.object.tuikuan1.toFixed(2));//总退款现金
 */			$("#shiji").text(result.object.shiji.toFixed(2));//总实际现金
		}else{
			Kit.alert("操作失败");return;
		}
	});
}

//兑换有效期”添加排序（可按时间大到小／小到大排序） 单选图片点击切换
var querySort=document.getElementsByClassName("querySort");//点击区域
var down=document.getElementsByClassName("down");
for(var i=0;i<querySort.length;i++){
	   //给每个class添加id标识(当索引)区分每个class
	   querySort[i].id=i;	   
	   //鼠标点击改变背景图片偏移
	   querySort[i].onclick=function(){
		   //当前索引
		   var index=this.id;
		   for( j=0;j<down.length;j++){
			   down[j].style.backgroundPosition="0px 61px";
		   }
		 //改变索引为当前的class背景图片偏移位置
		   down[index].style.backgroundPosition="0px 21px";
	   }
}
</script>
</body>
</html>